package com.glory.huaweipda.viewmodel

import androidx.databinding.ObservableArrayList
import com.glory.huaweipda.BR
import com.glory.huaweipda.R
import com.glory.huaweipda.bean.SpcitemByWafer
import com.glory.huaweipda.bean.WaferDataBean
import com.glory.huaweipda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.ItemBinding

class TrackOutModel : BaseViewModel(){

    var mfgItems = ObservableArrayList<SpcitemByWafer>()
    var spcList = ArrayList<SpcitemByWafer>()
    var itemBinding = ItemBinding.of<SpcitemByWafer>(BR.wafers, R.layout.item_out_layout)
    private val repository by lazy { TaskRepository() }


    fun defaultLotInfo(eqpID: String, user: String, lotID: String) {
        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("Eqpid", eqpID)
        map.addProperty("Opid", user)
//        map.addProperty("TrackStatus", "TRACKOUT")
        map.addProperty("TrackStatus", "")
        map.addProperty("IsUploadSpc", "")

        launchOnlyResult({
            val defaultLotInfo =
                repository.defaultLotInfo(map)
            if (defaultLotInfo.lots.isNotEmpty()){
                val wafer = defaultLotInfo.lots.get(0).waferList.wafer
                val spcitemByWafer = defaultLotInfo.lots.get(0).spclist.spcitemByWafer
                if (wafer.isNotEmpty()){
                    for (w in wafer.indices){
                        val sbw = SpcitemByWafer()
                        sbw.waferDataName = wafer[w].waferid
                        sbw.waferDataValue = wafer[w].slotno
                        spcList.add(sbw)
                    }
                }
                if (spcitemByWafer.isNotEmpty()) {
                    for (s in spcitemByWafer.indices) {
                        val sbw = SpcitemByWafer()
                        sbw.waferDataName = spcitemByWafer[s].waferDataName
                        sbw.waferDataValue = spcitemByWafer[s].waferDataValue
                        spcList.add(sbw)
                    }
                }
                mfgItems.addAll(spcList)
                mfgItems.addAll(spcList)
                defUI.callObserve.value = CallObserveBean(0, defaultLotInfo.lots.get(0))
            }else{
                defUI.toastEvent.value = ToastMessageBean("批次信息返回为空",false)
            }
        })
    }

    fun defaultTrackOut(eqpID: String, user: String, lotID: String){

        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("Eqpid", eqpID)
        map.addProperty("Opid", user)

        val w = WaferDataBean()
        w.lotid = lotID
        w.spcitem_ByLot = null
        w.spcitem_ByWafer = null
        map.add("Spclist", JsonParser().parse(Gson().toJson(w)).asJsonObject)

        launchOnlyResult({
            repository.defaultTrackOut(map)
            defUI.callObserve.value = CallObserveBean(200, "")
        })
    }

    fun defaultCancelTrackIn(eqpID: String, user: String, lotID: String,desc: String){

        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("Eqpid", eqpID)
        map.addProperty("Opid", user)
        map.addProperty("desc", desc)

        val w = WaferDataBean()
        w.lotid = lotID
        w.spcitem_ByLot = null
        w.spcitem_ByWafer = null
        map.add("Spclist", JsonParser().parse(Gson().toJson(w)).asJsonObject)

        launchOnlyResult({
            repository.defaultCancelTrackIn(map)
            defUI.callObserve.value = CallObserveBean(1)
        })
    }

}